# -*- coding: utf-8 -*-
from flask import Flask, render_template, request, url_for, redirect
import os,sys
from flask import Flask, flash, request, redirect, url_for
from werkzeug.utils import secure_filename
from flask import send_from_directory
from dbSqlite3 import *
import numpy as np
import tkinter as tk
from tkinter import filedialog
from flask import Flask, render_template, request, redirect, url_for, make_response, jsonify,session
from werkzeug.utils import secure_filename
import os
import cv2
import time

from datetime import timedelta
import xlrd

# filename=""

def xz():
    root = tk.Tk()
    root.withdraw()

    # Folderpath = filedialog.askdirectory()  # 获得选择好的文件夹
    Filepath = filedialog.askopenfilename()  # 获得选择好的文件

    # print('Folderpath:', Folderpath)
    print('Filepath:', Filepath)
    return Filepath

def get_data(filename,method='r'):
    '''
    改变数据结构 -- 方便前端显示
    :param filename:  文件名
    :param method:  按照 列或者 行 返回数据
    '''
    data = xlrd.open_workbook(filename)
    table= data.sheets()[0]
    nrows = table.nrows  # 行数
    ncols = table.ncols  # 列数
    if method == 'r':
        row_list = [ table.row_values(i) for i in range(0,nrows)]   # 所有行的数据
        return row_list
    elif method == 'c':
        col_list = [ table.col_values(i) for i in range(0,ncols)]    # 所有列的数据
        return col_list

app = Flask(__name__)
app.secret_key = 'abcdefgh!@#$%'

@app.route('/',methods=['POST','GET'])
def login():
    # if request.method=="GET":
        return render_template('cg_Login.html')
    # return redirect(url_for('index'))

@app.route('/check_class_details',methods=['POST','GET'])
def check_class_details():
    return render_template('check_class_details.html')
#excel表格录入
@app.route('/check_students_grades',methods=['POST','GET'])
def check_students_grades():
    courses, _ = GetSql2("select * from open_view where 教师编号='2017101001'")
    print(courses)
    filename=""
    if request.method == 'POST':
        f = request.files['file']
        print(request.files)
        user_input = request.form.get("name")
        basepath = os.path.dirname(__file__)  # 当前文件所在路径
        upload_path = os.path.join(basepath, 'static/images',
                                   secure_filename(f.filename))  # 注意：没有的文件夹一定要先创建，不然会提示没有该路径
        # upload_path = os.path.join(basepath, 'static/images','test.jpg')  #注意：没有的文件夹一定要先创建，不然会提示没有该路径
        f.save(upload_path)
        filename = upload_path
    if(filename!=""):
        print(filename)
        row_list=get_data(filename)
        # tablename = "course"
        # result, fields = GetSql2("select * from %s" % tablename)
        return render_template('check_students_grades.html', datas=row_list,courses=courses)
    else:
        return render_template('check_students_grades.html',courses=courses)

@app.route('/confirm_excel',methods=['POST','GET'])
def confirm_excel():
    if request.method == 'POST':
        m = request.form
        n=dict(m)
        for j in n.values():
            print(j)
        print("学生的学号是", n.keys())
        print("学生的分数是",n.values())
    return render_template('check_class_details.html')
#手动录入
@app.route('/see_students',methods=['GET','POST'])
def see_students():
    #老师教的所有课
    cno = request.args['his_class']
    courses, _ = GetSql2("select * from open_view where 教师编号='2017101001'")
    #cno = request.form.get('his_class')
    print("选择的课程是：",cno)

    students, _ = GetSql2("select * from choice_view where 课程号 ='%s'" % cno)

    return render_template('check_students_grades.html', datas=students,courses=courses)

if __name__ == '__main__':
    # app.run("0.0.0.0",debug=True)
    app.run(debug=True)
    # app.run()
